<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class MagicPHP  {
    public static $m_ext = 'model';
    public static $c_ext = 'controller';
    public static $ext  = '.php';
    public static $dot = '.';
    public static $_instance ; 
    public static function  callMethod(){
        self::$_instance = new self ; 
        return self::$_instance ;
    }
    public  function import($file,$extention = NULL ,$debug = false){
        $extention != ''  ? $extention = self::$dot.$extention : $extention = '.php';
        $fiileName = $file.$extention;
        if($debug == true) { echo $fiileName ; }
        if(file_exists($fiileName)) {
            include_once "$fiileName";
        } else {
              echo  new ErrorException($fiileName. "Is not exist.");
        }   
    }
    public function importLibarary($file,$extention=NULL,$debug = false){
        $this->import(MAGICPHP_LIB_PATH.$file,$extention,$debug);
    }
    public  function defineModel($element,$queryString){
        if(FLAG == 0) {
            $str = str_replace('q=index.php/','',$queryString);
            $mvc = explode('/',$str);
            if(sizeof($mvc)>= 3) {
            } elseif(sizeof($mvc)<= 1) {
            array_push($mvc[0]=DEFAULT_MODEL);
            array_push($mvc[1]=DEFAULT_ACTION);
            } elseif(sizeof($mvc)> 1 && sizeof($mvc)<3){
                array_push($mvc[1]=DEFAULT_ACTION);
            }
        } elseif (FLAG == 1)  {
            $qString = $_GET["q"];
            if(empty($qString)) {
                $count  = 0 ;
            } else {
                $mvc = explode("/",$qString);
                $count = sizeof($mvc) ;        
            }
            if($count == 0) {
                array_push($mvc[0]=DEFAULT_MODEL);
                array_push($mvc[1]=DEFAULT_CONTROLLER);
                array_push($mvc[2]=DEFAULT_ACTION);
            } elseif($count >=1 && $count < 2){
                array_push($mvc[1]=DEFAULT_CONTROLLER);
                array_push($mvc[2]=DEFAULT_ACTION);
            } elseif($count >=2 && $count  < 3 ){
                array_push($mvc[2]=DEFAULT_ACTION);
            }
        }
        return $mvc[$element] ; 
       
    }
    public  function getController(){
       return self::defineModel(0,$_SERVER["QUERY_STRING"])."_".self::$c_ext;
    }
    public  function getModel(){
       return self::defineModel(0,$_SERVER["QUERY_STRING"])."_".self::$m_ext;
    }
    public  function createObj(){
        $name = self::getModel();
        $$name = new $name ;
    }
    public  function getView(){
       return self::defineModel(1,$_SERVER["QUERY_STRING"]);
    }
    public  function getViewAction(){
        return self::defineModel(1,$_SERVER["QUERY_STRING"])."Controller";
    }
    public  function getAppDirectory(){
         return self::defineModel(0,$_SERVER["QUERY_STRING"]);
    } 
    public  function writeLog($message){
        $message = "[".date("d-m-Y h:i:s")."]  ".$message."\n";
        try { 
           // Open the file to get existing content
             $content = file_get_contents(LOG_PATH);
             
             //echo $content ; exit ;
             $fh = fopen(LOG_PATH, 'w') ; 
             fwrite($fh,  $content.$message);
             fclose($fh);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
    public function getBaseClass($class){
        if(get_parent_class($class) == 'base_controller' ){
            return true;
        } else {
            return false ;
        }
    }
    public function  copyObj($obj){
        global $view; 
        $view = $obj ;
    }
}

?>
